<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { r1 } from "@/api/project/report/r1";
import Cookies from 'js-cookie'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    }
  },
  data() {
    return {
      chart: null,
      aa: 0,
      bb: 0,
      cc: 0,
      query:"",
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.getnum();
      
    });
  },

  created() {
    
  },

  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    getnum() {
      if(unescape(Cookies.get("permissions")).indexOf(",pie,")<0){
        return;
      }
      r1(this.query).then(response => {
        this.aa=response.data[0];
        this.bb=response.data[1];
        this.cc=response.data[2];
        this.initChart();     
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        title: {
          text: "优惠券统计",
          x: "center",
          padding: [5,0,40,0],
          textStyle:{
                    color:'#0DB9F2',        //颜色
                    fontStyle:'normal',     //风格
                    fontWeight:'normal',    //粗细
                    fontFamily:'Microsoft yahei',   //字体
                    fontSize:16,     //大小
                    align:'center'   //水平对齐
                },
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        grid: {
          top: 0,
          left: "2%",
          right: "2%",
          bottom: "3%",
          containLabel: true
        },
        legend: {
          left: "center",
          bottom: "10",
          data: ["未使用", "已使用", "已过期"]
        },
        series: [
          {
            name: "优惠券统计",
            type: "pie",
            roseType: "radius",
            radius: [15, 95],
            center: ["50%", "50%"],
            data: [
              { value: this.aa, name: "未使用" },
              { value: this.bb, name: "已使用" },
              { value: this.cc, name: "已过期" }
            ],
            animationEasing: "cubicInOut",
            animationDuration: 2600
          }
        ]
      });
    }
  }
};
</script>
